package main;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.zip.DeflaterInputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
import org.apache.commons.compress.compressors.pack200.Pack200CompressorInputStream;
import org.apache.commons.compress.compressors.pack200.Pack200CompressorOutputStream;
import org.apache.commons.compress.compressors.xz.XZCompressorInputStream;
import org.apache.commons.compress.compressors.xz.XZCompressorOutputStream;
import org.msgpack.MessagePack;

import com.alibaba.fastjson.JSON;
import com.jcraft.jzlib.DeflaterOutputStream;

public class TestZip {
	
	public static JdkSerializer jdkSer = new JdkSerializer();
	public static FstSerialize fstSer = new FstSerialize();
	
	/***
	 * 压缩GZip
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] gZip(byte[] data) {
		byte[] b = null;
		try {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			GZIPOutputStream gzip = new GZIPOutputStream(bos);
			gzip.write(data);
			gzip.finish();
			gzip.close();
			b = bos.toByteArray();
			bos.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}

	/***
	 * 解压GZip
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] unGZip(byte[] data) {
		byte[] b = null;
		try {
			ByteArrayInputStream bis = new ByteArrayInputStream(data);
			GZIPInputStream gzip = new GZIPInputStream(bis);
			byte[] buf = new byte[1024];
			int num = -1;
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			while ((num = gzip.read(buf, 0, buf.length)) != -1) {
				baos.write(buf, 0, num);
			}
			b = baos.toByteArray();
			baos.flush();
			baos.close();
			gzip.close();
			bis.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}

	/***
	 * 压缩Zip
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] zip(byte[] data) {
		byte[] b = null;
		try {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			ZipOutputStream zip = new ZipOutputStream(bos);
			ZipEntry entry = new ZipEntry("zip");
			entry.setSize(data.length);
			zip.putNextEntry(entry);
			zip.write(data);
			zip.closeEntry();
			zip.close();
			b = bos.toByteArray();
			bos.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}

	/***
	 * 解压Zip
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] unZip(byte[] data) {
		byte[] b = null;
		try {
			ByteArrayInputStream bis = new ByteArrayInputStream(data);
			ZipInputStream zip = new ZipInputStream(bis);
			while (zip.getNextEntry() != null) {
				byte[] buf = new byte[1024];
				int num = -1;
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				while ((num = zip.read(buf, 0, buf.length)) != -1) {
					baos.write(buf, 0, num);
				}
				b = baos.toByteArray();
				baos.flush();
				baos.close();
			}
			zip.close();
			bis.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}

	/***
	 * 压缩BZip2
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] bZip2(byte[] data) {
		byte[] b = null;
		try {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			BZip2CompressorOutputStream bzip2 = new BZip2CompressorOutputStream(bos);
			bzip2.write(data);
			bzip2.flush();
			bzip2.close();
			b = bos.toByteArray();
			bos.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}

	/***
	 * 解压BZip2
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] unBZip2(byte[] data) {
		byte[] b = null;
		try {
			ByteArrayInputStream bis = new ByteArrayInputStream(data);
			BZip2CompressorInputStream bzip2 = new BZip2CompressorInputStream(bis);
			byte[] buf = new byte[1024];
			int num = -1;
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			while ((num = bzip2.read(buf, 0, buf.length)) != -1) {
				baos.write(buf, 0, num);
			}
			b = baos.toByteArray();
			baos.flush();
			baos.close();
			bzip2.close();
			bis.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}

	/**
	 * 把字节数组转换成16进制字符�?
	 * 
	 * @param bArray
	 * @return
	 */
	public static String bytesToHexString(byte[] bArray) {
		StringBuffer sb = new StringBuffer(bArray.length);
		String sTemp;
		for (int i = 0; i < bArray.length; i++) {
			sTemp = Integer.toHexString(0xFF & bArray[i]);
			if (sTemp.length() < 2)
				sb.append(0);
			sb.append(sTemp.toUpperCase());
		}
		return sb.toString();
	}

	/**
	 * jzlib 压缩数据
	 * 
	 * @param object
	 * @return
	 * @throws IOException
	 */
	public static byte[] jzlib(byte[] object) {

		byte[] data = null;
		try {
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			DeflaterOutputStream zOut = new DeflaterOutputStream(out);
			DataOutputStream objOut = new DataOutputStream(zOut);
			objOut.write(object);
			objOut.flush();
			zOut.close();
			data = out.toByteArray();
			out.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
		return data;
	}

	/**
	 * jzLib压缩的数�?
	 * 
	 * @param object
	 * @return
	 * @throws IOException
	 */
	public static byte[] unjzlib(byte[] object) {

		byte[] data = null;
		try {
			ByteArrayInputStream in = new ByteArrayInputStream(object);
			DeflaterInputStream zIn = new DeflaterInputStream(in);
			byte[] buf = new byte[1024];
			int num = -1;
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			while ((num = zIn.read(buf, 0, buf.length)) != -1) {
				baos.write(buf, 0, num);
			}
			data = baos.toByteArray();
			baos.flush();
			baos.close();
			zIn.close();
			in.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
		return data;
	}
	
	
	/**
	 * jzlib 压缩数据
	 * 
	 * @param object
	 * @return
	 * @throws IOException
	 */
	public static byte[] xz(byte[] object) {

		byte[] data = null;
		try {
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			XZCompressorOutputStream zOut = new XZCompressorOutputStream(out);
			DataOutputStream objOut = new DataOutputStream(zOut);
			objOut.write(object);
			objOut.flush();
			zOut.close();
			data = out.toByteArray();
			out.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
		return data;
	}

	/**
	 * jzLib压缩的数�?
	 * 
	 * @param object
	 * @return
	 * @throws IOException
	 */
	public static byte[] unxz(byte[] object) {

		byte[] data = null;
		try {
			ByteArrayInputStream in = new ByteArrayInputStream(object);
			XZCompressorInputStream zIn = new XZCompressorInputStream(in);
			byte[] buf = new byte[1024];
			int num = -1;
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			while ((num = zIn.read(buf, 0, buf.length)) != -1) {
				baos.write(buf, 0, num);
			}
			data = baos.toByteArray();
			baos.flush();
			baos.close();
			zIn.close();
			in.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
		return data;
	}

	
	
	/**
	 * jzlib 压缩数据
	 * 
	 * @param object
	 * @return
	 * @throws IOException
	 */
	public static byte[] pack200(byte[] object) {

		byte[] data = null;
		try {
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			Pack200CompressorOutputStream zOut = new Pack200CompressorOutputStream(out);
			DataOutputStream objOut = new DataOutputStream(zOut);
			objOut.write(object);
			objOut.flush();
			zOut.close();
			data = out.toByteArray();
			out.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
		return data;
	}

	/**
	 * jzLib压缩的数�?
	 * 
	 * @param object
	 * @return
	 * @throws IOException
	 */
	public static byte[] unpack200(byte[] object) {

		byte[] data = null;
		try {
			ByteArrayInputStream in = new ByteArrayInputStream(object);
			Pack200CompressorInputStream zIn = new Pack200CompressorInputStream(in);
			byte[] buf = new byte[1024];
			int num = -1;
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			while ((num = zIn.read(buf, 0, buf.length)) != -1) {
				baos.write(buf, 0, num);
			}
			data = baos.toByteArray();
			baos.flush();
			baos.close();
			zIn.close();
			in.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
		return data;
	}
	
	
	public static void main(String[] args) throws IOException {
		
		ArrayList<TestSerBean> list = new ArrayList();
		TestSerBean  b=null;
		for(int i =0;i<300;i++)
		{
			b = new TestSerBean();
			b.id  = i;
			b.a1  = (byte)i;
			b.a2  = (byte)i;
			b.a3  = (byte)i;
			b.a4  = (byte)i;
			list.add(b);
		}
		
		byte[] bs = fstSer.serialize(list);
		System.out.println(bs.length);
		
		MessagePack msgpack = new MessagePack();
		// Serialize
//		bs = msgpack.write(list);
		
//		bs = JSON.toJSONString(list).getBytes("UTF-8");
		
		for(int i =0;i<10;i++)
		{
			testLenth(bs);
		}
		tesSpeed(bs);
		
	}
	
	
	public static void  testLenth(byte[] bytes)
	{
		System.out.println("长度测试"+bytes.length);
		byte[] b1 = zip(bytes);
		unZip(b1);
		System.out.println("zip:" + b1.length);

		byte[] b3 = bZip2(bytes);
		unBZip2(b3);
		System.out.println("bZip2:" + b3.length);

		byte[] b5 = gZip(bytes);
		unGZip(b5);
		System.out.println("gZip:" + b5.length);

		byte[] b7 = jzlib(bytes);
		unjzlib(b7);
		System.out.println("jzlib:" + b7.length);
	
		
		byte[] b9 = xz(bytes);
		unxz(b9);
		System.out.println("xz: " + b9.length);
		
		byte[] b10 = pack200(bytes);
		unpack200(b10);
		System.out.println("pack200: " + b9.length);
	}
	
	
	public static void tesSpeed(byte[] bytes)
	{
		System.out.println("速度测试");
		long start= System.currentTimeMillis();
		
		for(int i =0;i<1000;i++)
		{
			byte[] b1 = zip(bytes);
			byte[] b2 = unZip(b1);
		}
		System.out.println("zip "+(System.currentTimeMillis()-start));
		start= System.currentTimeMillis();

		for(int i =0;i<1000;i++)
		{
			byte[] b3 = bZip2(bytes);
			byte[] b4 = unBZip2(b3);
		}
		
		System.out.println("bZip2 "+(System.currentTimeMillis()-start));
		start= System.currentTimeMillis();
		
		for(int i =0;i<1000;i++)
		{
			byte[] b5 = gZip(bytes);
			byte[] b6 = unGZip(b5);
		}
		
		System.out.println("gZip "+(System.currentTimeMillis()-start));
		start= System.currentTimeMillis();
		
		for(int i =0;i<1000;i++)
		{
			byte[] b7 = jzlib(bytes);
			byte[] b8 = unjzlib(b7);
		}
		
		System.out.println("jzlib "+(System.currentTimeMillis()-start));
		start= System.currentTimeMillis();
	
		
		for(int i =0;i<1000;i++)
		{
			byte[] b9 = xz(bytes);
			byte[] b10 = unxz(b9);
		}
		System.out.println("xz "+(System.currentTimeMillis()-start));
		start= System.currentTimeMillis();
		
		
		for(int i =0;i<1000;i++)
		{
			byte[] b10 = pack200(bytes);
			byte[] b11 = unpack200(b10);
		}
		System.out.println("pack200  "+(System.currentTimeMillis()-start));
		start= System.currentTimeMillis();
		
		
	}	
	
}
